package com.hzeport.bgs22emduty.inbound.api;

import cn.hutool.core.io.FileUtil;
import com.alibaba.nacos.common.utils.UuidUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzeport.bgs22emduty.config.login.UserInfoDetailVO;
import com.hzeport.bgs22emduty.entity.*;
import com.hzeport.bgs22emduty.enums.DeleteFlagEnum;
import com.hzeport.bgs22emduty.outbound.BaseCustomPhoneRepositoy;
import com.hzeport.bgs22emduty.outbound.BaseDeptInfoRepository;
import com.hzeport.bgs22emduty.outbound.ScreenDutyMapsRepository;
import com.hzeport.bgs22emduty.outbound.ScreenSubCustomRepository;
import com.hzeport.bgs22emduty.vo.file.FileVO;
import com.hzeport.bgs22emduty.common.base.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author zhangyangyang
 * @description
 * @date 2024/5/9 8:41
 */
@RestController
@AllArgsConstructor
@RequestMapping("/test")
@Api(value = "test", tags = "test管理")
@Slf4j
public class TestController {

    @Resource
    private BaseCustomPhoneRepositoy baseCustomPhoneRepositoy;
    @Resource
    private ScreenSubCustomRepository screenSubCustomRepository;
    @Resource
    private ScreenDutyMapsRepository screenDutyMapsRepository;
    @Resource
    private BaseDeptInfoRepository baseDeptInfoRepository;

    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param basePersonInfo ${comments}
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public void getBasePersonInfoPage() {
        List<ScreenDutyMaps> screenDutyMapsList = screenDutyMapsRepository.list();
        List<ScreenSubCustom> screenSubCustoms = screenSubCustomRepository.list();
        List<BaseCustomPhone> baseCustomPhones = new ArrayList<>();
        baseCustomPhoneRepositoy.remove(new LambdaQueryWrapper<>());
        for (ScreenDutyMaps map : screenDutyMapsList) {
            if (!StringUtils.isEmpty(map.getPhoneTel()) && !StringUtils.isEmpty(map.getNoWorkPhone())) {
                BaseCustomPhone workPhone = new BaseCustomPhone();
                workPhone.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                workPhone.setCustomId(map.getCustomCode());
                workPhone.setDayType("1");
                workPhone.setPhoneTel(map.getPhoneTel());
                workPhone.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
                workPhone.setCreateTime(LocalDateTime.now());
                workPhone.setCreateBy("sys");
                baseCustomPhoneRepositoy.save(workPhone);
                BaseCustomPhone noWorkPhone = new BaseCustomPhone();
                noWorkPhone.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                noWorkPhone.setCustomId(map.getCustomCode());
                noWorkPhone.setDayType("3");
                noWorkPhone.setPhoneTel(map.getPhoneTel());
                noWorkPhone.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
                noWorkPhone.setCreateTime(LocalDateTime.now());
                noWorkPhone.setCreateBy("sys");
                baseCustomPhoneRepositoy.save(noWorkPhone);
            }
            if (!StringUtils.isEmpty(map.getPhoneTel()) && StringUtils.isEmpty(map.getNoWorkPhone())) {
                BaseCustomPhone workPhone = new BaseCustomPhone();
                workPhone.setId(UUID.randomUUID().toString().replaceAll("-", ""));

                workPhone.setCustomId(map.getCustomCode());
                workPhone.setDayType("1");
                workPhone.setPhoneTel(map.getPhoneTel());
                workPhone.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
                workPhone.setCreateTime(LocalDateTime.now());
                workPhone.setCreateBy("sys");
                baseCustomPhoneRepositoy.save(workPhone);
            }
        }
        for (ScreenSubCustom map : screenSubCustoms) {
            if (!StringUtils.isEmpty(map.getPhone()) && !StringUtils.isEmpty(map.getNoWorkPhone())) {
                BaseCustomPhone workPhone = new BaseCustomPhone();
                workPhone.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                workPhone.setCustomId(map.getId());
                workPhone.setDayType("1");
                workPhone.setPhoneTel(map.getPhone());
                workPhone.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
                workPhone.setCreateTime(LocalDateTime.now());
                workPhone.setCreateBy("sys");
                baseCustomPhoneRepositoy.save(workPhone);
                BaseCustomPhone noWorkPhone = new BaseCustomPhone();
                noWorkPhone.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                noWorkPhone.setCustomId(map.getId());
                noWorkPhone.setDayType("3");
                noWorkPhone.setPhoneTel(map.getNoWorkPhone());
                noWorkPhone.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
                noWorkPhone.setCreateTime(LocalDateTime.now());
                noWorkPhone.setCreateBy("sys");
                baseCustomPhoneRepositoy.save(noWorkPhone);
            }
            if (!StringUtils.isEmpty(map.getPhone()) && StringUtils.isEmpty(map.getNoWorkPhone())) {
                BaseCustomPhone workPhone = new BaseCustomPhone();
                workPhone.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                workPhone.setCustomId(map.getId());
                workPhone.setDayType("1");
                workPhone.setPhoneTel(map.getPhone());
                workPhone.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
                workPhone.setCreateTime(LocalDateTime.now());
                workPhone.setCreateBy("sys");
                baseCustomPhoneRepositoy.save(workPhone);
            }
        }
    }


    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param basePersonInfo ${comments}
     * @return
     */
    @ApiOperation(value = "getUserDept", notes = "getUserDept")
    @GetMapping("/getUserDept")
    public void getUserDept() {

    }


    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param basePersonInfo ${comments}
     * @return
     */
    @ApiOperation(value = "上传", notes = "上传")
    @PostMapping("/createCustom")
    public BaseResult<FileVO> createCustom() throws IOException {
        String filePath = "D:\\svn\\202324\\02开发资料\\全国海关值班联络表0528\\全国海关值班联络表0528.xls";
        FileInputStream fileInputStream = new FileInputStream(filePath);
        Workbook workbook = new HSSFWorkbook(fileInputStream);
        Sheet sheet = workbook.getSheetAt(0);
        String customName;
        String deptName;
        BaseDeptInfo custom=null;
        BaseDeptInfo subCustom;
        ScreenDutyMaps screenDutyMaps=null;
        ScreenSubCustom screenSubCustom=null;
        List<ScreenDutyMaps> screenDutyMapsList=new ArrayList<>();
        List<ScreenSubCustom> screenSubCustomList=new ArrayList<>();
        for (int rowNum = 1; rowNum < sheet.getLastRowNum(); rowNum++) {
            Row row = sheet.getRow(rowNum);
            if (row != null) {
                Cell cell = row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                if (cell != null) {
                    DataFormatter dataFormatter = new DataFormatter();
                    String tempName=dataFormatter.formatCellValue(cell);
                    if (tempName.contains("海关")&&!tempName.contains("1")&&!tempName.contains("2")) {
                        tempName.replaceAll("各隶属海关值班联络表","各隶属海关值班联络表");
                        customName=tempName;
                        custom=baseDeptInfoRepository.getOne(new LambdaQueryWrapper<BaseDeptInfo>()
                        .eq(BaseDeptInfo::getDeptName,customName)
                        .eq(BaseDeptInfo::getParentid,"94480300-fca2-4268-86c2-4c13963bf4cb"));
                        if (custom==null){
                            continue;
                        }
                        screenDutyMaps=screenDutyMapsRepository.getOne(new LambdaQueryWrapper<ScreenDutyMaps>()
                        .eq(ScreenDutyMaps::getCustomCode,custom.getId()),false);
                        if (screenDutyMaps!=null){
                            continue;
                        }
                        screenDutyMaps.setCustomCode(custom.getId());
                        screenDutyMaps.setCustomName(custom.getCustomName());
                        screenDutyMaps.setCreateBy("sys20241118");
                        screenDutyMaps.setDelFlag("0");
                        screenDutyMapsList.add(screenDutyMaps);
                        System.out.println(tempName);
                    }
                    if (tempName.contains("总关值班室电话")||tempName.contains("总关值班电话")){
                        tempName=tempName.replaceAll(" ","").replaceAll("总关值班传真：","，")
                                .replaceAll("总关值班电话：","")
                                .replaceAll("总关值班室电话：","");
                        String workPhone=tempName.split("，")[0];
                        String workFax=tempName.split("，")[1];
                        screenDutyMaps.setPhoneTel(workPhone);
                        screenDutyMaps.setFax(workFax);
                        System.out.println(workPhone+" "+workFax);
                    }
                }
                 cell = row.getCell(1,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                if (custom==null){
                    continue;
                }
                    if (cell!=null){
                        DataFormatter dataFormatter=new DataFormatter();
                        deptName=dataFormatter.formatCellValue(cell);
                        System.out.println(deptName);
                        subCustom=baseDeptInfoRepository.getOne(new LambdaQueryWrapper<BaseDeptInfo>()
                                .eq(BaseDeptInfo::getDeptName,deptName)
                                .eq(BaseDeptInfo::getParentid,custom.getId()),false);
                        if (subCustom==null){
                            continue;
                        }
                        screenSubCustom=screenSubCustomRepository.getById(subCustom.getId());
                        if (screenSubCustom!=null){
                            continue;
                        }
                        screenSubCustom=new ScreenSubCustom();
                        BeanUtils.copyProperties(subCustom,screenSubCustom);
                        screenSubCustom.setParentid(custom.getId());
                        screenSubCustom.setCreateBy("sys20241118");
                        screenSubCustom.setDelFlag("0");
                        screenSubCustomList.add(screenSubCustom);
                        System.out.println(deptName);
                    }
                    cell = row.getCell(2,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                    if (cell!=null){
                     DataFormatter dataFormatter=new DataFormatter();
                        screenSubCustom.setPhone(dataFormatter.formatCellValue(cell));
                        System.out.println(dataFormatter.formatCellValue(cell));
                     }
                }

//                    BaseDeptInfo baseDeptInfo=baseDeptInfoRepository.getOne(new LambdaQueryWrapper<BaseDeptInfo>()
//                            .eq(BaseDeptInfo::getDeptName,cell.getStringCellValue()));
//                    log.info(""+baseDeptInfo);
//                }
//             }
        }
        screenSubCustomRepository.saveBatch(screenSubCustomList);
        screenDutyMapsRepository.saveBatch(screenDutyMapsList);
        return BaseResult.ok();
    }
}
